#include "Misc.hpp"

namespace misc {

bool stringEndsWith(const std::string &s, const std::string &ending) {
	if (ending.length() > s.length())
		return false;
	return s.substr(s.length() - ending.length(), ending.length()) == ending;
}

cv::Mat exchangeRowsWithColumns(const cv::Mat &input) {
	cv::Mat out;
	cv::transpose(input, out);
	return out;
}

std::ostream &printMatrix(const cv::Mat &m, std::ostream &stream) {
	const std::streamsize oldPrec = stream.precision();
	stream.setf(std::ios::fixed, std::ios::floatfield);
	stream.precision(1);

	cv::Mat md;
	m.convertTo(md, CV_64F);

	stream << "Matrix (" << m.cols << " x " << m.rows << ")" << std::endl;
	stream << "[";

	for (int y = 0; y < md.rows; ++y) {
		for (int x = 0; x < md.cols; ++x) {
			stream << md.at<double>(cv::Point(x, y));
			if (x != md.cols - 1) {
				stream << ", ";
			}
		}

		if (y < md.rows - 1) {
			stream << ";" << std::endl;
		} else {
			stream << "]";
		}
	}

	stream.precision(oldPrec);
	return stream;
}

std::string currentTimeAsString() {
	time_t rawtime;
	struct tm * timeinfo;
	char buffer[256];

	time(&rawtime);
	timeinfo = localtime(&rawtime);

	strftime(buffer, 256, "%d-%m-%Y %I:%M:%S:%f", timeinfo);
	return std::string(buffer);
}

}
